﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ChineseSchool.BLL;
using ChineseSchool.Models.Hethong;
using System.Web.UI.HtmlControls;

namespace ChineseSchool.Admin
{
    public partial class ImageUpload : System.Web.UI.UserControl
    {
        public B_Image B_Image { get; set; }
        public B_Insert_Update_Delete bll { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            B_Image = new B_Image();
            bll = new B_Insert_Update_Delete();
            if (!IsPostBack)
            {
                EditIndex = -1;                
            }
            LoadData();
        }

        public void LoadData()
        {
            Repeater1.DataSource = B_Image.ListImage;
            Repeater1.DataBind();
        }

        protected void OnAddRecord(object sender, EventArgs e)
        {
            /*tải ảnh */
            bool success = false;
            string ImageUrl = "";
            HttpPostedFile file2 = fuImageUrl.PostedFile;
            if (file2 == null)
            {
                lblThongbao.Text = "File đã tồn tại! Vui lòng chọn file khác";
            }
            if (fuImageUrl.HasFile == false && file2.ContentLength > 500000)
            {
                lblThongbao.Text = "File nặng quá T_T";
            }
            else
            {
                try
                {
                    string path2 = Server.MapPath("/admin/images/Upload/" + fuImageUrl.FileName);
                    fuImageUrl.SaveAs(path2);
                    ImageUrl = "/admin/images/Upload/" + fuImageUrl.FileName.ToString();
                    lblThongbao.Text = "Tải thành công";
                    success = true;
                }
                catch
                {
                    lblThongbao.Text = "Tải ảnh thất bại! vui lòng tải lại";
                }
            }

            /*tải ảnh */
            if (success == true)
            {
                //  No point in adding anything if empty
                if (!string.IsNullOrWhiteSpace(txtImageName.Text) || !string.IsNullOrWhiteSpace(txtLink.Text))
                {
                    // Add a new Contact and rebind the repeater

                    string imageName = txtImageName.Text;
                    string imageUrl = ImageUrl;
                    string description = txtDesc.Text;
                    string link = txtLink.Text;
                    M_Image M_Image = new M_Image(imageName, imageUrl, description, link);
                    B_Image.ListImage.Add(M_Image);

                    Repeater1.DataSource = B_Image.ListImage;
                    Repeater1.DataBind();
                    txtImageName.Text = "";
                    txtDesc.Text = "";
                    txtLink.Text = "";
                }
                else
                {
                    lblThongbao.Text = "Vui lòng nhập đầy đủ thông tin liên quan!";
                }
            }
        }

        protected void OnItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                B_Image.ListImage.RemoveAt(e.Item.ItemIndex);
            }
            else if (e.CommandName == "edit")
            {
                EditIndex = e.Item.ItemIndex;
            }
            else if (e.CommandName == "save")
            {
                HtmlInputHidden t = e.Item.FindControl("ImageNameHidden") as HtmlInputHidden;
                B_Image.ListImage[e.Item.ItemIndex].imageUrl = t.Value;
                t = e.Item.FindControl("LinkHidden") as HtmlInputHidden;
                B_Image.ListImage[e.Item.ItemIndex].link = t.Value;
                t = e.Item.FindControl("DescHidden") as HtmlInputHidden;
                B_Image.ListImage[e.Item.ItemIndex].description = t.Value;
                EditIndex = -1;
            }

            Repeater1.DataSource = B_Image.ListImage;
            Repeater1.DataBind();
        }

        protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                if (e.Item.ItemIndex == EditIndex)
                {
                    // Find the placeholder
                    PlaceHolder p = e.Item.FindControl("ImageNameEditPlaceholder") as PlaceHolder;
                    // Create textBox and assign the current value of the data item
                    TextBox t = new TextBox();
                    t.ID = "ImageNameEdit";
                    t.Text = ((M_Image)e.Item.DataItem).imageName;
                    // Add the textbox to the placeholder
                    p.Controls.Add(t);
                    // Get the existing label and hide it
                    Label l = e.Item.FindControl("ImageName") as Label;
                    l.Visible = false;


                    p = e.Item.FindControl("LinkEditPlaceholder") as PlaceHolder;
                    t = new TextBox();
                    t.ID = "LinkEdit";
                    t.Text = ((M_Image)e.Item.DataItem).link;
                    p.Controls.Add(t);

                    l = e.Item.FindControl("Link") as Label;
                    l.Visible = false;


                    p = e.Item.FindControl("DescEditPlaceholder") as PlaceHolder;
                    t = new TextBox();
                    t.ID = "DescEdit";
                    t.Text = ((M_Image)e.Item.DataItem).description;
                    p.Controls.Add(t);

                    l = e.Item.FindControl("Desc") as Label;
                    l.Visible = false;


                    // Make hidden fields visible
                    HtmlInputHidden h = e.Item.FindControl("ImageNameHidden") as HtmlInputHidden;
                    h.Visible = true;
                    h = e.Item.FindControl("LinkHidden") as HtmlInputHidden;
                    h.Visible = true;
                    h = e.Item.FindControl("DescHidden") as HtmlInputHidden;
                    h.Visible = true;

                    // Remove the edit button from display
                    ImageButton b = e.Item.FindControl("Edit") as ImageButton;
                    b.Visible = false;

                    // Resuse the delete button
                    b = e.Item.FindControl("Delete") as ImageButton;
                    b.CommandName = "save";
                    b.OnClientClick = "OnSave(this)";
                    b.ImageUrl = "~/admin/images/base_floppydisk_32.png";
                }
            }
        }

        private int EditIndex
        {
            get { return (int)ViewState["EditIndex"]; }
            set { ViewState["EditIndex"] = value; }
        }
    }
}
